//
//  OAuthViewController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/7.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit
import SVProgressHUD
class OAuthViewController: UIViewController {
    
    lazy var webView = UIWebView()
    

    
    
    override func loadView() {
        view = webView
        view.backgroundColor = UIColor.whiteColor()
        
        webView.delegate = self
        
        title = "新浪微博登陆"
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: self, action: "close")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: UIBarButtonItemStyle.Plain, target: self, action: "autoFill")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

    webView.loadRequest(NSURLRequest(URL: NetworkTools.shareNetworkTools.oauthURL))
//        print(NetworkTools.shareNetworkTools.oauthURL)
    }

}

// MARK: - 代理方法
extension OAuthViewController:UIWebViewDelegate{
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
//        print(request)
        //保证之后的requestURL.host = 百度
        guard let requestURL = request.URL where requestURL.host == "www.baidu.com" else{
            return true
        }
        //保证执行到下一步requestURL.query是以"code="开头的
        guard let requestQuery = requestURL.query where requestQuery.hasPrefix("code=") else{
            //取消授权关闭页面
            close()
            return false
        }
//        print(requestQuery)
        let code = requestQuery.substringFromIndex("code=".endIndex)
        //获取令牌token并且申请userInfo
        UserAccountViewModel.shareAccountModel.loadAccessTokenAndUserInfo(code) { (isSuccess) -> () in
            if isSuccess {
                print("成功加载")
                self.dismissViewControllerAnimated(true, completion: { () -> Void in
                    SVProgressHUD.dismiss()
                    NSNotificationCenter.defaultCenter().postNotificationName(LQChangeRootViewController, object: "welcome")
                })
            } else {
//                print("加载失败")
                SVProgressHUD.showInfoWithStatus("您的网络不给力啊")
            }
        }
        return false
    }
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
}


///navigationItem事件
extension OAuthViewController{
    @objc private func close () {
        SVProgressHUD.dismiss()
        dismissViewControllerAnimated(true, completion: nil)
    }
    @objc private func autoFill () {

        let js = "document.getElementById('userId').value = '451481634@qq.com';" +
        "document.getElementById('passwd').value = '1wang2huan';"
//        let js = "document.getElementById('userId').value = 'daoge10000@sina.cn';" +
//        "document.getElementById('passwd').value = 'qqq123';"
        
        webView.stringByEvaluatingJavaScriptFromString(js)
    }
}

